package pl.prv.polanie.client.panels.lists;

import pl.prv.polanie.client.dialogs.CreateGroupDialog;
import pl.prv.polanie.client.dto.CSGroupDTO;
import pl.prv.polanie.client.dto.CSVGroupDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.CacheService;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.widgets.table.TableGroups;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class GroupsList extends ObjectsList {

	TableGroups tableGroups = new TableGroups(this);

	public GroupsList() {
		
		CSGroupDTO[] ls = Cache.getAllGroups();
		//AcademicTitleDTO[] ats = Cache.getAllAcademicTitles();
		
		add(tableGroups);
		
		for (int i = 0; i < ls.length; i++) {
			
			CSGroupDTO cs = ls[i];
			CSVGroupDTO csv = ViewDataProvider.getCSVGroup(cs);
			
			tableGroups.addRow(cs, csv);
		}
		
		
		addRecordButton.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				if (CacheService.isPossibleToCreateGroup()) {
					CreateGroupDialog cld = new CreateGroupDialog(GroupsList.this, null); 
					cld.center();
				} else {
					Window.alert("You cannot create new Group until you create at least one Semester Year Group.");
				}
			}
		});
		
		removeSelectedRecordButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				tableGroups.removeSelectedGroupFromTable();
			}
		});
		
		setupButtons();
	}

	
	public void addCSGroup(CSGroupDTO csGroup) {
//		CSVGroupDTO csv = ViewDataProvider.getCSVGroup(csGroup);
		
		tableGroups.saveOrUpdateGroupInTable(csGroup);
//		tableGroups.addRow(csGroup, csv);
	}
	
	public void removeSelectdCSGroup() {
		tableGroups.removeSelectedGroupFromTable();
	}
	
}
